package hxg.thread.safethread;

import java.rmi.ServerRuntimeException;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author HeXiangGuang
 * @create 2021-06-24 22:50
 */
public class Demo5 {
    public static void main(String[] args) throws Exception{
        ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<>();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    queue.offer(i);
                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 6; i < 10; i++) {
                    try {
                        queue.offer(i);
                    } catch (Exception e) {
                        e.printStackTrace();
                        try {
                            throw new ServerRuntimeException("",null);
                        } catch (ServerRuntimeException serverRuntimeException) {
                            serverRuntimeException.printStackTrace();
                        }
                    }
                }
            }
        });
        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();
        System.out.println("出队-----");
        int size = queue.size();
        for (int i = 0; i < size; i++) {
            System.out.println(queue.poll());
        }
    }
}
